<?php
namespace addon\mall\controller;
use app\common\controller\AddonAdminBase;
use tpfcore\Core;
// +----------------------------------------------------------------------
// | Author: yaoyihong <510974211@qq.com>
// +----------------------------------------------------------------------
/**
 * ============================================================================
 * 版权所有 2017-2077 tpframe工作室，并保留所有权利。
 * @link http://www.tpframe.com/
 * @copyright Copyright (c) 2017 TPFrame Software LLC
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！未经本公司授权您只能在不用于商业目的的前提下对程序代码进行修改和使用；
 * 不允许对程序代码以任何形式任何目的的再发布
 * ============================================================================
 */
class GoodsAttribute extends AddonAdminBase
{
    public function index(){
      	$this->addonTemplate('index',[
          "list"=>Core::loadAddonModel($this->name)->getGoodsAttribute([
          		"order"=>"id asc",
          		'paginate'=>['rows' => 10,'simple'=>false,'config'=>['query'=>$this->param]],
              'where'=>['type_id'=>isset($this->param['type_id']) && $this->param['type_id']>0?$this->param['type_id']:[">",0]],

          ]),
          "type_id"=>isset($this->param['type_id'])?$this->param['type_id']:0,
          "listGoodsType"=>Core::loadAddonModel("GoodsType")->getGoodsType([
              "order"=>"id asc"
          ])
      	]);
    }
    public function add(){
      IS_POST && $this->jump(Core::loadAddonModel($this->name)->saveGoodsAttribute($this->param));
      if(isset($this->param['id'])){
        $this->assign("list",Core::loadAddonModel($this->name)->getGoodsAttribute(["where"=>["id"=>$this->param['id']]]));
      }
      $this->addonTemplate('add_edit',[
        "type_id"=>isset($this->param['type_id'])?$this->param['type_id']:0,
        "listGoodsType"=>Core::loadAddonModel("GoodsType")->getGoodsType([
              "order"=>"id asc"
          ])
      ]);
    }
    public function del(){
    	 $this->jump(Core::loadAddonModel($this->name)->delGoodsAttribute($this->param));
    }
    public function getByTypeId(){
      $listGoodsAttr = isset($this->param['goods_id'])?Core::loadAddonModel("GoodsAttr")->getGoodsAttr(["where"=>["goods_id"=>$this->param['goods_id']]]):[];
      $listGoodsAttribute = Core::loadAddonModel($this->name)->getGoodsAttribute([
          "order"=>"id asc",
          "where"=>['type_id'=>$this->param['type_id']]
        ]);



      foreach ($listGoodsAttribute as $key => $value) {
        $cache_arr=[];
        $listGoodsAttribute[$key]=$value->toArray();
        foreach ($listGoodsAttr as $k => $v) {
          if($value['id'] == $v['attr_id']){
            $cache_arr[]=$v->toArray();
            $listGoodsAttribute[$key]['goods_attr']=$cache_arr;
          }
        }
      }

      /*echo "<pre/>";
      print_r($listGoodsAttribute);

      die;*/

      return $this->addonTemplate('list',[
        "listGoodsAttribute" => $listGoodsAttribute
      ]);
    }
}
